// 请求验证中间件
export const validateAvatarRequest = (req, res, next) => {
  const { type, gender, expression, age, customDescription, personality } = req.body;
  
  // 验证必需字段
  if (!type || !gender || !expression) {
    return res.status(400).json({
      success: false,
      message: '缺少必需参数: type, gender, expression'
    });
  }
  
  // 验证type字段
  const validTypes = ['engineer', 'technician', 'architect', 'consultant'];
  if (!validTypes.includes(type)) {
    return res.status(400).json({
      success: false,
      message: `无效的type值: ${type}。有效值: ${validTypes.join(', ')}`
    });
  }
  
  // 验证gender字段
  const validGenders = ['male', 'female', 'neutral'];
  if (!validGenders.includes(gender)) {
    return res.status(400).json({
      success: false,
      message: `无效的gender值: ${gender}。有效值: ${validGenders.join(', ')}`
    });
  }
  
  // 验证expression字段
  const validExpressions = ['normal', 'thinking', 'explaining', 'smiling', 'serious', 'happy'];
  if (!validExpressions.includes(expression)) {
    return res.status(400).json({
      success: false,
      message: `无效的expression值: ${expression}。有效值: ${validExpressions.join(', ')}`
    });
  }
  
  // 验证age字段（可选）
  if (age !== undefined) {
    const ageNum = parseInt(age);
    if (isNaN(ageNum) || ageNum < 18 || ageNum > 80) {
      return res.status(400).json({
        success: false,
        message: 'age必须是18-80之间的数字'
      });
    }
  }
  
  // 验证personality字段（可选）
  if (personality && Array.isArray(personality)) {
    const validPersonalities = ['幽默风趣', '严谨专业', '耐心细致', '积极乐观'];
    const invalidPersonalities = personality.filter(p => !validPersonalities.includes(p));
    if (invalidPersonalities.length > 0) {
      return res.status(400).json({
        success: false,
        message: `无效的personality值: ${invalidPersonalities.join(', ')}。有效值: ${validPersonalities.join(', ')}`
      });
    }
  }
  
  // 验证customDescription字段（可选）
  if (customDescription && typeof customDescription !== 'string') {
    return res.status(400).json({
      success: false,
      message: 'customDescription必须是字符串'
    });
  }
  
  // 如果customDescription不为空，检查长度
  if (customDescription && customDescription.length > 500) {
    return res.status(400).json({
      success: false,
      message: 'customDescription长度不能超过500字符'
    });
  }
  
  next();
};
